//
//  ViewModelDemo.swift
//  SwiftUIDemo
//
//  Created by Tilt Wang on 2025/7/3.
//

import SwiftUI

struct FruitModel: Identifiable {
    let id: String = UUID().uuidString
    let name: String
    let count: Int
}

class FruitViewModel: ObservableObject {
    @Published var fruits: [FruitModel] = []
    @Published var isLoading: Bool = false
    
    init() {
        getFruits()
    }
    
    func getFruits() {
        let fruit1 = FruitModel(name: "Bananas", count: 5)
        let fruit2 = FruitModel(name: "Watermellons", count: 88)
        fruits.append(fruit1)
        fruits.append(fruit2)
        isLoading = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.fruits.append(fruit1)
            self.fruits.append(fruit2)
            self.isLoading = false
        }
    }
    
    func deleteFruit(indexSet: IndexSet) {
        fruits.remove(atOffsets: indexSet)
    }
}

struct ViewModelDemo: View {
    
//    @State var fruits: [FruitModel] = [
//        FruitModel(name: "Apples", count: 44),
//        FruitModel(name: "Oranges", count: 33)
//    ]
//    @ObservedObject var fruitViewModel: FruitViewModel = FruitViewModel()
    //@StateObject 创建或初始化的对象 使用这个
    //@ObservedObject 二级页面或子页面使用这个
    @StateObject var fruitViewModel: FruitViewModel = FruitViewModel()
    
    var body: some View {
        NavigationView {
            List {
                if fruitViewModel.isLoading {
                    ProgressView()
                } else {
                    ForEach(fruitViewModel.fruits) { fruit in
                        HStack {
                            Text("\(fruit.count)")
                                .foregroundColor(.red)
                            Text(fruit.name)
                                .font(.headline)
                                .bold()
                        }
                    }
                    //                .onDelete { indexSet in
                    //                    fruitViewModel.deleteFruit(indexSet: indexSet)
                    //                }
                    .onDelete(perform: fruitViewModel.deleteFruit)
                }
            }
            .listStyle(GroupedListStyle())
            .navigationTitle("Fruit List")
            .navigationBarItems(trailing:
                                    NavigationLink(destination: OtherScreenView(fruitViewModel: fruitViewModel), label: {
                
                Image(systemName: "arrow.right")
                    .font(.title)
            })
            )
//            .onAppear {
//                fruitViewModel.getFruits()
//            }
        }
    }

}

struct OtherScreenView: View {

    @ObservedObject var fruitViewModel: FruitViewModel
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        ZStack {
            Color.green.ignoresSafeArea()
            VStack {
                ForEach(fruitViewModel.fruits) { fruit in
                    HStack {
                        Text(fruit.name)
                            .font(.headline)
                            .bold()
                    }
                }
            }
//            Button {
//                presentationMode.wrappedValue.dismiss()
//            } label: {
//                Text("Go Back")
//                    .foregroundColor(.white)
//                    .font(.largeTitle)
//                    .fontWeight(.semibold)
//            }

        }
    }
}

#Preview {
    ViewModelDemo()
}
